package com.aos.eap.activity;

import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.RepairBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 更换劈刀
 */
public class EE_ChangeToolingActivity extends BaseDialogActivity {


    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.rbType1)
    RadioButton rbType1;
    @BindView(R.id.rbType2)
    RadioButton rbType2;
    @BindView(R.id.etNewToolingId)
    FreeUI_ClearEditText etNewToolingId;
    @BindView(R.id.tvEqpId)
    TextView tvEqpId;
    @BindView(R.id.tvLotId)
    TextView tvLotId;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private RepairBean repairBean;
    private String type = "热压板";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ee_change_tooling;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        repairBean = (RepairBean) getIntent().getSerializableExtra("repairBean");
        tvEqpId.setText(repairBean.getEqpId());
        tvLotId.setText(repairBean.getLotId());
    }

    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("Tooling更换");
        freeUI_titleView.setLeftListener(view -> showBackDialog(EE_ChangeToolingActivity.this));
        freeUI_titleView.setRightListener("提交", v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            String toolId = etNewToolingId.getText().toString().trim();
            if (FreeApi_StringUtils.isEmpty(toolId)) {
                showToast("请扫描Tooling编号", false);
                return;
            }

            JsonObject map = new JsonObject();
            map.addProperty("EqpID", repairBean.getEqpId());
            map.addProperty("LotID", repairBean.getLotId());
            map.addProperty("ToolID", toolId);
//            map.addProperty("ToolType", type);
            map.addProperty("Remark", etRemark.getText().toString().trim());
            requestSubmit(map);
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_RepaireTool", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false,getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    new Handler().postDelayed(() -> {
                        setResult(2102);
                        EE_ChangeToolingActivity.this.finish();
                    }, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_RepaireTool");
            }
        });
    }


    @OnClick({R.id.rbType1, R.id.rbType2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rbType1:
                type = "热压板";
                break;
            case R.id.rbType2:
                type = "加热块";
                break;
        }
    }
}
